package cn.lang.oss;

import cn.lang.oss.factory.IOssFactory;
import cn.lang.oss.factory.QiniuOssFactory;
import cn.lang.oss.factory.UpyunOssFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @ClassName : OssKit
 * @Description : 工具类
 * @Author : Lang
 * @Date: 2020-03-07 13:30
 */
public class OssKit {

    private static IOssFactory defaultOssFactory;

    private static Oss oss;

    static {
        init();
    }

    private static void init(){
        loadConfig();
        if (defaultOssFactory == null) {
            defaultOssFactory = new QiniuOssFactory();
        }
        if(oss == null){
            oss = defaultOssFactory.getOss();
        }
    }

    public static void setDefaultOssFactory(IOssFactory defaultOssFactory){
        if (defaultOssFactory == null) {
            throw new IllegalArgumentException("defaultOssFactory can not be null.");
        }
        OssKit.defaultOssFactory = defaultOssFactory;
    }

    public static void setOss(Oss oss) {
        if (oss == null) {
            throw new IllegalArgumentException("oss can not be null.");
        }
        OssKit.oss = oss;
    }

    /**
     * @Description 默认配置
     * @author Lang
     * @Date 2020/6/19 22:41
     */
    private static void loadConfig(){
        Properties resources = getResources();
        if(resources==null){
            return;
        }
        String sdk = resources.getProperty("oss.sdk");
        if(OssConstants.QINIU.equals(sdk)||sdk==null||sdk==""){
            defaultOssFactory = new QiniuOssFactory( resources.getProperty("oss.qiniu.accessKey"),
                    resources.getProperty("oss.qiniu.secretKey"),
                    resources.getProperty("oss.qiniu.bucket"),
                    resources.getProperty("oss.qiniu.domain"));
        }
        if(OssConstants.UPY.equals(sdk)){
            defaultOssFactory = new UpyunOssFactory(resources.getProperty("oss.upy.username"),
                    resources.getProperty("oss.upy.password"),
                    resources.getProperty("oss.upy.bucket"),
                    resources.getProperty("oss.upy.domain"));
        }
    }

    /**
     * @Description 加载配置文件
     * @author Lang
     * @Date 2020/6/19 22:41
     */
    public static Properties getResources(){
        // 获取输入流
        InputStream in = OssKit.class.getClassLoader().getResourceAsStream("oss.properties");
        // 新建配置对象
        Properties p = new Properties();
        // 通过输入流加载
        try {
            p.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 读取key
        return  p;
    }

    public static String upload(File targetFile){
        return  oss.upload(targetFile);
    }

    public static String upload(File targetFile, String resourcesName){
        return  oss.upload(targetFile,resourcesName);
    }

    public static String upload(String targetName, String resourcesName){
        return  oss.upload(targetName,resourcesName);
    }

    public static String upload(byte[] data,String targetName){
        return  oss.upload(data,targetName);
    }




}
